//
//  ItemSecondNextViewController.m
//  Lovelife
//
//  Created by apple on 15/4/27.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "ItemSecondNextViewController.h"
#import "ItemSecondNextModel.h"
#import "ItemSecondNextAnotherModel.h"
#import "AFNetworking.h"
#import "UIImageView+WebCache.h"
#import "ItemSecondModel.h"
#import "ItemSecondAnotherCell.h"
#import "WebViewController.h"
#import "ItemSecondAnotherTopView.h"
#import "AFNetWorkingYU.h"
#import "MJRefresh.h"
#import "ItemZeroModel.h"
#import "NightManager.h"
@interface ItemSecondNextViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,retain)NSMutableArray *nextModel;//大图片及其简介

@property (nonatomic,retain)NSMutableArray *nextAnotherModel;//tableviewcell 数据

@property (nonatomic,retain)UIActivityIndicatorView *acIntor;

@property (nonatomic,retain)UITableView *tableView;

@property (nonatomic,retain)UIScrollView *scroll;

@property (nonatomic,retain)ItemSecondAnotherTopView *top;

@property (nonatomic,assign)BOOL temp;

@property (nonatomic,assign)NSInteger number;



@end

@implementation ItemSecondNextViewController

-(void)dealloc
{
//    [_nextAnotherModel release];
//    [_nextModel release];
    [_acIntor release];
    [_ID release];
    [super dealloc];

}

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    
    if (self) {
        
        self.nextModel = [NSMutableArray array];
        
        self.nextAnotherModel = [NSMutableArray array];
       
        self.number = 1;
        
        self.temp = YES;

        

    }
    return self;


}

-(void)getData
{
    if (self.temp == YES) {
            [self.acIntor startAnimating];
    }

    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    [manager GET:[NSString stringWithFormat:@"http://ibaby.ipadown.com/api/food/food.topic.detail.php?id=%@",self.ID] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        ItemSecondNextModel *next = [[ItemSecondNextModel alloc]init];
        
       
        
        for (NSDictionary *dic in responseObject[@"tlist"]) {
            
            
            [next setValuesForKeysWithDictionary:dic];
            
            [self.nextModel addObject:next];
            
            self.top.jianjie.text = next.jianjie;
            
            self.top.title.text = next.title;
            
            [next release];
            
        }
        
#pragma mark 给top LabelText赋值
        
        
        for (ItemSecondModel *sec in self.aboveDataModel) {
            if (sec.ID == self.ID) {
                self.top.titleTop.text = sec.title;
                
                [self.top.Img sd_setImageWithURL:[NSURL URLWithString:sec.thumb]];
                
                self.top.jianjieTop.text = sec.jianjie;
            }
        }

        for (NSMutableDictionary *dic in responseObject[@"tlist"]) {
            
            NSMutableArray *tempArr = dic[@"list"];
            
            for (NSMutableDictionary *dic in tempArr) {
                
                ItemSecondNextAnotherModel *another = [[ItemSecondNextAnotherModel alloc]init];
                [another setValuesForKeysWithDictionary:dic];
                
                [self.nextAnotherModel addObject:another];
                
                [another release];
                
                
            }
            
        }
        
        if (self.temp == YES) {
            [self.acIntor stopAnimating];
            [self createView];
        }
        
        [self.tableView reloadData];

   
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"错误信息%@",error);
    }];


}

-(void)createAcIntor
{
    self.acIntor = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    [self.acIntor setCenter:self.view.center];
    [self.acIntor setBackgroundColor:[UIColor grayColor]];
    [self.acIntor setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhiteLarge];
    [self.view addSubview:self.acIntor];
    [_acIntor release];
    
}
-(void)createView
{


    self.top.backgroundColor = [UIColor whiteColor];
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    self.tableView.tableHeaderView = self.top;
    
    self.tableView.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:self.tableView];
    
    [_tableView release];
    
    self.tableView.delegate = self;
    
    self.tableView.dataSource = self;
    
//    [self.tableView addHeaderWithCallback:^{
//        
//        [self.nextModel removeAllObjects];
//        
//        [self.tableView reloadData];
//        
//        [self.nextAnotherModel removeAllObjects];
//        
//        [self.tableView reloadData];
//        
//        [self getData];
//        
//        [self.tableView headerEndRefreshing];
//        
//    
//    }];
//    
//    [self.tableView addFooterWithCallback:^{
//        
//        self.number ++;
//        
//        self.temp = NO;
//        
//        [self getData];
//        
//        [self.tableView footerEndRefreshing];
//    }];


}




- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self createAcIntor];
     self.top = [[ItemSecondAnotherTopView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 600)];
    [self getData];


    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return self.nextAnotherModel.count;

}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuse = @"reuse";
    
    ItemSecondAnotherCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    
    if (!cell) {
        cell = [[[ItemSecondAnotherCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse]autorelease];
    }
    ItemSecondNextAnotherModel *another = self.nextAnotherModel[indexPath.row];
    
    [cell.Img sd_setImageWithURL:[NSURL URLWithString:another.thumb]];
    
    cell.category.text = another.category;
    
    cell.age.text = another.age;
    
    cell.effect.text = another.effect;
    
    cell.title.text = another.title;
    
    return cell;

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    return self.view.frame.size.height/5+20;

}



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    for (NSInteger i = 0; i < self.nextAnotherModel.count; i++) {
        if (indexPath.row == i) {
            
            ItemZeroModel *zero = self.nextAnotherModel[indexPath.row];
            
            WebViewController *web = [[WebViewController alloc]init];
            
            zero.url = [NSString stringWithFormat:@"http://ibaby.ipadown.com/api/food/food.show.detail.php?id=%@",self.ID];
            
            web.model = zero;
            
            web.url = [NSString stringWithFormat:@"http://ibaby.ipadown.com/api/food/food.show.detail.php?id=%@",self.ID];
            
            
            
            [self.navigationController pushViewController:web animated:YES];
            
            [web release];
        }
    }


}
-(void)setNightDayModel
{
    [NightManager setBackgroundColorWithView:self.view];
    
}

-(void)setLightDayModel
{
    [NightManager setBackgroundColorWithView:self.view];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
